package com.hzdesk.media.koreabatchmatvector.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.media.SoundPool;
import android.os.Environment;
import android.os.Handler;
import android.os.storage.StorageManager;
import android.util.Log;


import com.hzdesk.media.koreabatchmatvector.utils.DeleteDirTest;
import com.hzdesk.media.koreabatchmatvector.utils.FileUtil;
import com.hzdesk.media.koreabatchmatvector.utils.MyToastUtils;
import com.hzdesk.media.koreabatchmatvector.utils.RxBus;

import java.io.File;
import java.io.IOException;

/**
 * SD卡挂载监听
 * Created by Administrator on 2017/12/6.
 */

public class SDCardReceiver extends BroadcastReceiver {
    private static final String TAG = "SDCardReceiver";
    public static final String MEDIA_UNMOUNTED = "android.intent.action.MEDIA_UNMOUNTED";
    public static final String MEDIA_MOUNTED = "android.intent.action.MEDIA_MOUNTED";
    private boolean flag = true;


    @Override
    public void onReceive(final Context context, final Intent intent) {
            String action = intent.getAction();
            if (action.equals(MEDIA_MOUNTED)) {                                                     //拔出 UsbManager.ACTION_USB_DEVICE_ATTACHED
                MyToastUtils.ShortToaseShow(context," 检测到usb设备");
                // todo 弹出进度框
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        String uri =  intent.getData().getPath();
                        if(uri != null){
                            traverseWaterMark(uri,context);
                        }
                    }
                },1000);
            }else if (action.equals(MEDIA_UNMOUNTED)) {                                             //插入 UsbManager.ACTION_USB_DEVICE_DETACHED
                MyToastUtils.ShortToaseShow(context," 检测到usb弹出");
            }
    }
    /**
     * 遍历 usb
     * */
    public void traverseWaterMark(final String url,Context context) {
        boolean flag = false;
        try {
            File[] files = new File(url).listFiles();
            for (File file : files) {
              if(file.isDirectory()){
                    if(file.getName().equals("Desk_Media_Water_Mark")){
                        // 遍历该目录下的文件 拷贝到 sdCard    Watermark/watermark/ 目录下
                        flag = true;
                        traverseFile(file.getAbsolutePath(),context);
                    }else{
                      String path = file.getAbsolutePath();
                        traverseWaterMark(path,context);
                    }
              }
            }
        } catch (Exception e) {
            MyToastUtils.ShortToaseShow(context," 拷贝水印失败");
        }finally {
            if(!flag){
                MyToastUtils.ShortToaseShow(context," 未检测到水印");
            }else{
                MyToastUtils.ShortToaseShow(context," 水印拷贝完成");
            }

           // todo  发消息给设置logo页面刷新数据；
        }
    }
    /**
     * @desc 遍历水印文件
     * */
    private void traverseFile(String absolutePath,Context context) {
        File[] files = new File(absolutePath).listFiles();
        for (File file : files) {
            if(file.isFile() && file.exists()){
                //  拷贝文件 到sdCard    Watermark/watermark/ 目录下
                try {
                    String uil = DeleteDirTest.getPath("Watermark")+"/watermark";
                    String path = uil+"/"+file.getName();
                    FileUtil.copyFile(file,path);
                } catch (IOException e) {
                    Log.e(TAG, "traverseFile: 拷贝失败" +e);
                }
            }
        }
    }


}
